package com.torchclx.etcdstarter;

import io.etcd.jetcd.ByteSequence;
import io.etcd.jetcd.Client;
import io.etcd.jetcd.kv.GetResponse;
import io.etcd.jetcd.kv.PutResponse;
import org.springframework.data.redis.connection.BitFieldSubCommands;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.ValueOperations;

import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

public class EtcdValueOperations implements ValueOperations<String, String> {

    private Client client;
    public EtcdValueOperations (Client client) {
        this.client = client;
    }

    @Override
    public void set(String s, String s2) {

        ByteSequence from = ByteSequence.from(s, StandardCharsets.UTF_8);
        ByteSequence value = ByteSequence.from(s2, StandardCharsets.UTF_8);
        CompletableFuture<PutResponse> put = client.getKVClient().put(from, value);
        try {
            put.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String get(Object o) {
        ByteSequence from = ByteSequence.from(o.toString(), StandardCharsets.UTF_8);
        CompletableFuture<GetResponse> getResponseCompletableFuture = client.getKVClient().get(from);
        GetResponse getResponse;
        try {
            getResponse = getResponseCompletableFuture.get();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        long count = getResponse.getCount();
        if (count > 0) {
            String string = getResponse.getKvs().get(0).getValue().toString(StandardCharsets.UTF_8);
            return string;
        }else {
            return "没有数据";
        }

    }

    @Override
    public String getAndDelete(String key) {
        return "";
    }

    @Override
    public String getAndExpire(String key, long timeout, TimeUnit unit) {
        return "";
    }

    @Override
    public String getAndExpire(String key, Duration timeout) {
        return "";
    }

    @Override
    public String getAndPersist(String key) {
        return "";
    }

    @Override
    public void set(String s, String s2, long l, TimeUnit timeUnit) {

    }

    @Override
    public Boolean setIfAbsent(String s, String s2) {
        return null;
    }

    @Override
    public Boolean setIfAbsent(String s, String s2, long l, TimeUnit timeUnit) {
        return null;
    }

    @Override
    public Boolean setIfPresent(String s, String s2) {
        return null;
    }

    @Override
    public Boolean setIfPresent(String s, String s2, long l, TimeUnit timeUnit) {
        return null;
    }

    @Override
    public void multiSet(Map<? extends String, ? extends String> map) {

    }

    @Override
    public Boolean multiSetIfAbsent(Map<? extends String, ? extends String> map) {
        return null;
    }



    @Override
    public String getAndSet(String s, String s2) {
        return "";
    }

    @Override
    public List<String> multiGet(Collection<String> collection) {
        return Collections.emptyList();
    }

    @Override
    public Long increment(String s) {
        return 0L;
    }

    @Override
    public Long increment(String s, long l) {
        return 0L;
    }

    @Override
    public Double increment(String s, double v) {
        return 0.0;
    }

    @Override
    public Long decrement(String s) {
        return 0L;
    }

    @Override
    public Long decrement(String s, long l) {
        return 0L;
    }

    @Override
    public Integer append(String s, String s2) {
        return 0;
    }

    @Override
    public String get(String s, long l, long l1) {
        return "";
    }

    @Override
    public void set(String s, String s2, long l) {

    }

    @Override
    public Long size(String s) {
        return 0L;
    }

    @Override
    public Boolean setBit(String s, long l, boolean b) {
        return null;
    }

    @Override
    public Boolean getBit(String s, long l) {
        return null;
    }

    @Override
    public List<Long> bitField(String s, BitFieldSubCommands bitFieldSubCommands) {
        return Collections.emptyList();
    }

    @Override
    public RedisOperations<String, String> getOperations() {
        return null;
    }
}
